using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbSanPhamDAL
    {
        private SqlConnection cn = null;
        public tbSanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbSanPhamDAL(tbSanPhamDAL sanpham)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public int Insert(tbSanPham sanpham)
        {
            SqlCommand cm = new SqlCommand("sp_tbSanPham_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenSP", sanpham.TenSP);
            cm.Parameters.AddWithValue("@DanhMuc", sanpham.DanhMuc);
            cm.Parameters.AddWithValue("@DoanhNghiep", sanpham.DoanhNghiep);
            cm.Parameters.AddWithValue("@NhaSX", sanpham.NhaSX);
            cm.Parameters.AddWithValue("@GiaGoc", sanpham.GiaGoc);
            cm.Parameters.AddWithValue("@GiaBan", sanpham.GiaBan);
            cm.Parameters.AddWithValue("@DiaDiem", sanpham.DiaDiem);
            cm.Parameters.AddWithValue("@NgayBatDau", sanpham.NgayBatDau);
            cm.Parameters.AddWithValue("@NgayHetHan", sanpham.NgayHetHan);
            cm.Parameters.AddWithValue("@SoLuongBan", sanpham.SoLuongBan);
            cm.Parameters.AddWithValue("@SoLuongToiDa", sanpham.SoLuongToiDa);
            cm.Parameters.AddWithValue("@MoTa", sanpham.MoTa);
            cm.Parameters.AddWithValue("@QuyDinhSuDung", sanpham.QuyDinhSuDung);
            cm.Parameters.AddWithValue("@ChietKhau", sanpham.ChietKhau);
            cm.Parameters.AddWithValue("@TinhTrang", sanpham.TinhTrang);
            cm.Parameters.AddWithValue("@PhuongThucThanhToan", sanpham.PhuongThucThanhToan);
            cm.Parameters.AddWithValue("@LoiGioiThieu", sanpham.LoiGioiThieu);
            return int.Parse(cm.ExecuteScalar().ToString());
        }
        public void Update(tbSanPham sanpham)
        {
            SqlCommand cm = new SqlCommand("sp_tbSanPham_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaSP", sanpham.MaSP);
            cm.Parameters.AddWithValue("@TenSP", sanpham.TenSP);
            cm.Parameters.AddWithValue("@DanhMuc", sanpham.DanhMuc);
            cm.Parameters.AddWithValue("@DoanhNghiep", sanpham.DoanhNghiep);
            cm.Parameters.AddWithValue("@NhaSX", sanpham.NhaSX);
            cm.Parameters.AddWithValue("@GiaGoc", sanpham.GiaGoc);
            cm.Parameters.AddWithValue("@GiaBan", sanpham.GiaBan);
            cm.Parameters.AddWithValue("@DiaDiem", sanpham.DiaDiem);
            cm.Parameters.AddWithValue("@NgayBatDau", sanpham.NgayBatDau);
            cm.Parameters.AddWithValue("@NgayHetHan", sanpham.NgayHetHan);
            cm.Parameters.AddWithValue("@SoLuongBan", sanpham.SoLuongBan);
            cm.Parameters.AddWithValue("@SoLuongToiDa", sanpham.SoLuongToiDa);
            cm.Parameters.AddWithValue("@MoTa", sanpham.MoTa);
            cm.Parameters.AddWithValue("@QuyDinhSuDung", sanpham.QuyDinhSuDung);
            cm.Parameters.AddWithValue("@ChietKhau", sanpham.ChietKhau);
            cm.Parameters.AddWithValue("@TinhTrang", sanpham.TinhTrang);
            cm.Parameters.AddWithValue("@PhuongThucThanhToan", sanpham.PhuongThucThanhToan);
            cm.Parameters.AddWithValue("@LoiGioiThieu", sanpham.LoiGioiThieu);
            cm.ExecuteNonQuery();
        }

        //public void UpdateTT(tbSanPham sanpham)
        //{
        //    SqlCommand cm = new SqlCommand("UpdateTinhTrangSP", cn);
        //    cm.CommandType = CommandType.StoredProcedure;
        //    cm.Parameters.AddWithValue("@MaSP", sanpham.MaSP);
        //    cm.Parameters.AddWithValue("@TinhTrang", sanpham.TinhTrang);
        //    cm.ExecuteNonQuery();
        //}
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbSanPham_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaSP", iD);
            cm.ExecuteNonQuery();
        }
        public tbSanPhamCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbSanPham_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbSanPhamCollection spCollection = new tbSanPhamCollection();

            while (dr.Read())
            {
                tbSanPham sanpham = new tbSanPham();
                sanpham.MaSP = int.Parse(dr.GetValue(0).ToString());
                sanpham.TenSP = dr.GetValue(1).ToString();
                sanpham.DanhMuc = int.Parse(dr.GetValue(2).ToString());
                sanpham.DoanhNghiep = int.Parse(dr.GetValue(3).ToString());
                sanpham.NhaSX = int.Parse(dr.GetValue(4).ToString());
                sanpham.GiaGoc = float.Parse(dr.GetValue(5).ToString());
                sanpham.GiaBan = float.Parse(dr.GetValue(6).ToString());
                sanpham.DiaDiem =int.Parse ( dr.GetValue(7).ToString());
                sanpham.NgayBatDau = DateTime .Parse (dr.GetValue(8).ToString());
                sanpham.NgayHetHan = DateTime .Parse ( dr.GetValue(9).ToString());
                sanpham.SoLuongBan = int.Parse(dr.GetValue(10).ToString());
                sanpham.SoLuongToiDa = int.Parse(dr.GetValue(11).ToString());
                sanpham.MoTa = dr.GetValue(12).ToString();
                sanpham.QuyDinhSuDung = dr.GetValue(13).ToString();
                sanpham.ChietKhau = float.Parse(dr.GetValue(14).ToString());
                sanpham.TinhTrang = int.Parse (dr.GetValue(15).ToString());
                sanpham.PhuongThucThanhToan = int.Parse(dr.GetValue(16).ToString());
                sanpham.LoiGioiThieu = dr.GetValue(17).ToString();
                spCollection.Add(sanpham);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return spCollection;
        }
    }
}
